import io
import json

from PIL.Image import Image
from fastapi import FastAPI, File
from starlette.responses import Response

app = FastAPI()


@app.get('/notify/v1/health')
def get_health():
    """
   Usage on K8S
   readinessProbe:
       httpGet:   path: /notify/v1/health
           port: 80
   livenessProbe:
       httpGet:
           path: /notify/v1/health
           port: 80
   :return:
       dict(msg='OK')
   """
    return dict(msg='OK')


# @app.post("/object-to-json")
# async def detect_food_return_json_result(file: bytes = File(...)):
#     input_image = get_image_from_bytes(file)
#     results = model(input_image)
#     detect_res = results.pandas().xyxy[0].to_json(orient="records")  # JSON img1 predictions
#     detect_res = json.loads(detect_res)
#     return {"result": detect_res}
#
#
# @app.post("/object-to-img")
# async def detect_food_return_base64_img(file: bytes = File(...)):
#     input_image = get_image_from_bytes(file)
#     results = model(input_image)
#     results.render()  # updates results.imgs with boxes and labels
#     for img in results.imgs:
#         bytes_io = io.BytesIO()
#         img_base64 = Image.fromarray(img)
#         img_base64.save(bytes_io, format="jpeg")
#     return Response(content=bytes_io.getvalue(), media_type="image/jpeg")